package com.chenfan.ccp.util.tool;


import com.chenfan.ccp.util.start.ApplicationContextUtil;
import com.chenfan.infra.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class PushUtil {

    private PushUtil() {

    }

    public static PushUtil getInstance() {
        return PushUtilHolder.INSTANCE;
    }

    public void send(PushType pushType, String templateName, String msg) {
        String os = System.getProperty("os.name");
        // 判断当前的环境
        if (!StringUtils.isEmpty(os) && os.startsWith("Linux")) {
            pushType.sendMsg(templateName, msg);
        }
    }

    /**
     * 先简单写出来
     */
    public enum PushType implements PushNotify {
        DINGDING {
            @Override
            public boolean sendMsg(String templateName, String msg) {
                Map<String, Object> param = new HashMap<>();
                param.put("msgtype", "markdown");
                Map<String, String> markDownParam = new HashMap<>();
                markDownParam.put("title", "系统异常提醒");
                markDownParam.put("text", msg);
                param.put("markdown", markDownParam);
                Map<String, Object> atParam = new HashMap<>();
                atParam.put("isAtAll", true);
                param.put("at", atParam);

                String result;

                //关键字告警
                if (ApplicationContextUtil.isProd()) {
                    result = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=8b29b3870cd2539203fbd728c1d8ef03e9c4ca0351cb20d84ffd0241538b331d",
                            JsonUtil.obj2String(param));
                } else {
                    result = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=5e8c3fb86dd65db54cb3b253aefc5e4a4054a39af9dd0b6f1fd87197ef9c0915",
                            JsonUtil.obj2String(param));
                }

                Map map = JsonUtil.string2Obj(result, Map.class);
                return map.getOrDefault("errcode", 0).equals(0);
            }
        }, WEIXIN {
            @Override
            public boolean sendMsg(String templateName, String msg) {
                return false;
            }
        }


    }


    public interface PushNotify {
        boolean sendMsg(String templateName, String msg);
    }

    private static class PushUtilHolder {
        private static final PushUtil INSTANCE = new PushUtil();

    }


}
